<?php
namespace Com\Mvc\Model;

use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Where;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;

class BaseModel extends TableGateway{

	/**
	 * 查询整张数据表
	 * @param array $columns
	 * @return array
	 */
	public function getRowList($columns = null){
		$select = $this->getSql()->select();
		if(is_array($columns)){
			$select->columns($columns);
		}
		$result = $this->selectWith($select)->toArray();
			
		return $result;
	}

	/**
	 * 翻页专用
	 * @param array|where $where
	 * @param array $columns
	 * @param array $order
	 * @param int $itemCountPerPage 每一页的数据数量
	 * @param int $currentPageNumber 当前显示第几页
	 */
	public function getPaginatorList($where=null,$columns=null,$order=null,$itemCountPerPage=10,$currentPageNumber=1){
		
		$select = $this->getSql()->select();
		
		if(is_array($columns)){
			$select->columns($columns);
		}
		
		if(is_array($where) || $where instanceof where){
			$select->where($where);
		}
		
		if (is_array($order)) {
			$select->order($order);
		}
		
		if(!is_numeric($itemCountPerPage) || $itemCountPerPage <= 0){
			throw new \Exception("每页显示数据量参数设置不符合要求！");
		}
		
		if(!is_numeric($currentPageNumber) || $currentPageNumber <= 0){
			throw new \Exception("当前页数参数设置不符合要求！");
		}
		
		$paginatorAdapter = new DbSelect($select, $this->adapter);
		$paginator = new Paginator($paginatorAdapter);
		$paginator->setItemCountPerPage($itemCountPerPage);
		$paginator->setCurrentPageNumber($currentPageNumber);
		
		$content = array();
		
		foreach ($paginator as $k=>$v){
			$content[$k] = (Array)$v;
		}
		
		$data = array(
			'page'=>$paginator,
			'pageContent'=>$content
		);
		
		return $data;
			
	}
}

?>